//
//  WYMainController.m
//  newsWY
//
//  Created by my mac computer on 2017/3/18.
//  Copyright © 2017年 my mac computer. All rights reserved.
//

#import "WYMainController.h"

@interface WYMainController ()

@end

@implementation WYMainController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    //给主框架添加子控制器
    [self addChildViewControllers];
}


/**
 添加所有的子控制器
 */
-(void)addChildViewControllers
{
    NSArray *childPramameter = @[
                                 
                                 @{@"clsName":@"WYHomeController",
                                   @"title":@"首页",
                                   @"icon":@"news"
                                       },
                                 
                                 @{@"clsName":@"WYHomeController",
                                   @"title":@"阅读",
                                   @"icon":@"reader"
                                   },
                                 
                                 @{@"clsName":@"WYHomeController",
                                   @"title":@"视频",
                                   @"icon":@"media"
                                   },
                                 
                                 @{@"clsName":@"WYHomeController",
                                   @"title":@"话题",
                                   @"icon":@"bar"
                                   },
                                 
                                 @{@"clsName":@"WYHomeController",
                                   @"title":@"我",
                                   @"icon":@"me"
                                   }
                                 ];
    
    //创建可变数组保存子控制器
    NSMutableArray *children = [NSMutableArray array];
    
    //循环遍历
    for (NSDictionary*dic in childPramameter)
    {
        //将所有子控制器添加到可变数组中
        UINavigationController *child = [self childrenController:dic];
        
        [children addObject:child];
    }
    
    //将可变数组中的子控制器添加到根控制器中
    self.viewControllers =children;
}




/**
 创建单个子控制器

 @param dic 子控制器参数
 */
-(UINavigationController*) childrenController:(NSDictionary*)dic
{
    //获取子控制器类名，生成子控制器
    NSString *clsName = dic[@"clsName"];
    
    UIViewController *controller = [NSClassFromString(clsName) new];
    
    
    //设置子控制器的标题属性
    controller.title =dic[@"title"];
    
    [controller.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} forState:UIControlStateSelected];
    
    //设置子控制器的tabbarItem的图片和选中图片颜色
    //正常状态下图片
    UIImage *image = [[UIImage imageNamed:[NSString stringWithFormat:@"tabbar_icon_%@_normal",dic[@"icon"]]]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    controller.tabBarItem.image =image;
    
    //选中状态下图片
    UIImage *selectedImage = [[UIImage imageNamed:[NSString stringWithFormat:@"tabbar_icon_%@_highlight",dic[@"icon"]]]
                              imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    controller.tabBarItem.selectedImage =selectedImage;
    
    
    //将子控制器设为navigationController的根控制器
    UINavigationController*nav = [[UINavigationController alloc]initWithRootViewController:controller];
    
    
    return nav;
}


















@end
